package cn.iocoder.yudao.module.trade.controller.admin.receiptinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.receiptinfo.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.receiptinfo.ReceiptInfoDO;
import cn.iocoder.yudao.module.trade.service.receiptinfo.ReceiptInfoService;

@Tag(name = "管理后台 - 单据信息")
@RestController
@RequestMapping("/trade/receipt-info")
@Validated
public class ReceiptInfoController {

    @Resource
    private ReceiptInfoService receiptInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建单据信息")
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:create')")
    public CommonResult<Long> createReceiptInfo(@Valid @RequestBody ReceiptInfoSaveReqVO createReqVO) {
        return success(receiptInfoService.createReceiptInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单据信息")
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:update')")
    public CommonResult<Boolean> updateReceiptInfo(@Valid @RequestBody ReceiptInfoSaveReqVO updateReqVO) {
        receiptInfoService.updateReceiptInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单据信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:delete')")
    public CommonResult<Boolean> deleteReceiptInfo(@RequestParam("id") Long id) {
        receiptInfoService.deleteReceiptInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:query')")
    public CommonResult<ReceiptInfoRespVO> getReceiptInfo(@RequestParam("id") Long id) {
        ReceiptInfoDO receiptInfo = receiptInfoService.getReceiptInfo(id);
        return success(BeanUtils.toBean(receiptInfo, ReceiptInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单据信息分页")
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:query')")
    public CommonResult<PageResult<ReceiptInfoRespVO>> getReceiptInfoPage(@Valid ReceiptInfoPageReqVO pageReqVO) {
        PageResult<ReceiptInfoDO> pageResult = receiptInfoService.getReceiptInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReceiptInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单据信息 Excel")
    @PreAuthorize("@ss.hasPermission('trade:receipt-info:export')")
    @OperateLog(type = EXPORT)
    public void exportReceiptInfoExcel(@Valid ReceiptInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiptInfoDO> list = receiptInfoService.getReceiptInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单据信息.xls", "数据", ReceiptInfoRespVO.class,
                        BeanUtils.toBean(list, ReceiptInfoRespVO.class));
    }

}